package com.frejoys.common.config;

import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@RequiredArgsConstructor
@ConditionalOnClass(WxPayService.class)
public class WxPayConfigManager {

    private final WxPayProperties properties;

    @Bean
    @ConditionalOnMissingBean
    public WxPayService wxPayService() {
        WxPayConfig payConfig = new WxPayConfig();
        payConfig.setAppId(properties.getAppid());
        payConfig.setMchId(properties.getMchid());
        payConfig.setMchKey(properties.getMchkey());
        payConfig.setApiV3Key(properties.getMchAPIv3Key());
        // payConfig.setKeyPath(properties.getKeypath());
        payConfig.setPrivateCertPath(properties.getPrivateCert());
        payConfig.setPrivateKeyPath(properties.getPrivateKey());
        payConfig.setKeyPath(properties.getP12Key());
        payConfig.setNotifyUrl(properties.getNotifyUrl());
        payConfig.setPayScoreNotifyUrl(properties.getRefundNotifyUrl());

        WxPayService wxPayService = new WxPayServiceImpl();
        wxPayService.setConfig(payConfig);
        return wxPayService;
    }

}
